package org.example.datascreen.exception;

import org.apache.shiro.authz.UnauthenticatedException;
import org.example.datascreen.eneity.Result;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e){
        e.printStackTrace();
        return Result.error(StringUtils.hasLength(e.getMessage())? e.getMessage() : "操作失败");
    }
    @ResponseBody
    @ExceptionHandler(UnauthenticatedException.class)
    public ResponseEntity<Result> handleUnauthenticatedException(UnauthenticatedException ex) {
        Result result = Result.error("请先登录");
        return new ResponseEntity<>(result, HttpStatus.UNAUTHORIZED);
    }

}
